package com.banciyuan.bcywebview.biz.groupdetail.groupmember;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.api.GroupApi;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.base.helper.ActionbarHelper;
import com.banciyuan.bcywebview.base.helper.ProgressbarHelper;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.view.pulltorefresh.PullToRefreshBase;
import com.banciyuan.bcywebview.base.view.pulltorefresh.PullToRefreshListView;
import com.banciyuan.bcywebview.biz.follow.FollowAdapter;
import com.banciyuan.bcywebview.biz.pc.zone.smooth.SmoothPersonActivity;
import com.banciyuan.bcywebview.utils.http.BcyPair;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.OnErrorProxy;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.StringRequestParam;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import de.greenrobot.daoexample.model.MineInfo;

/**
 * Created by hy on 2015/9/8.
 */
public class GroupMemberActivity extends BaseActivity implements ActionbarHelper.OnActionCallbacks {

    private String mGid = "";
    private RequestQueue mQueue;
    private View mProgressbarView;
    private ProgressbarHelper mProgressbarHelper;
    private ActionbarHelper mActionbarHelper;
    private View mActionBarView;
    private PullToRefreshListView refreshListView;
    private ListView mListView;
    private List<MineInfo> list = new ArrayList<>();
    private FollowAdapter adapter;
    private String member_num;

    private boolean is_load;
    private boolean is_end;
    private int page = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.groupmembers_layout);
        initArgs();
        initProgressbar();
        initActionbar();
        initUi();
        initAction();
        initData();
    }

    @Override
    protected void initArgs() {
        mQueue = VolleyQueue.getRquest(this);
        mGid = StringUtil.getString(getIntent(), gotoUtil.INTENT_VALUE_ONE);
        member_num = StringUtil.getString(getIntent(), gotoUtil.INTENT_VALUE_TWO);
    }

    @Override
    protected void initProgressbar() {
        mProgressbarView = findViewById(R.id.base_progressbar);
        mProgressbarHelper = new ProgressbarHelper(mProgressbarView);
        mProgressbarHelper.setActionCallbacks(new ProgressbarHelper.OnProgressCallbacks() {
            @Override
            public void onActionClick(int id) {
                mProgressbarHelper.onLoading();
                page = 1;
                initData();
            }
        });
        mProgressbarHelper.onLoading();
    }

    @Override
    protected void initActionbar() {
        mActionBarView = findViewById(R.id.base_action_bar);
        mActionbarHelper = new ActionbarHelper(this, mActionBarView);
        mActionbarHelper.setTitle(String.format(getString(R.string.nums_focus_talk), member_num));
        mActionbarHelper.setActionCallbacks(this);
    }

    @Override
    protected void initUi() {
        refreshListView = (PullToRefreshListView) findViewById(R.id.lv);
        mListView = refreshListView.getRefreshableView();
    }

    @Override
    protected void initAction() {
        refreshListView.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
            @Override
            public void onLastItemVisible() {
                if (!is_load && !is_end) {
                    page++;
                    initData();
                }
            }
        });
        refreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                is_load = false;
                is_end = false;
                page = 1;
                initData();
            }
        });
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                if (position < 1) {
                    position = 1;
                }
                position = position - 1;
                MineInfo mineInfo = (MineInfo) adapter.getItem(position);
                if (null == mineInfo)
                    return;
                gotoUtil.gotoAct(GroupMemberActivity.this, SmoothPersonActivity.class, mineInfo.getUid());
            }
        });
    }

    @Override
    protected void initData() {
        is_load = true;
        String url = HttpUtils.BASE_URL + GroupApi.getMembers();
        List<BcyPair> paramsList = new ArrayList<>();
        paramsList.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(this).getToken()));
        paramsList.add(new BcyPair(HttpUtils.GID, mGid));
        paramsList.add(new BcyPair(HttpUtils.LIMIT, "10"));
        paramsList.add(new BcyPair(HttpUtils.INDEX_P, "" + page));
        final HashMap<String, String> params = HttpUtils.getData(paramsList);

        Response.Listener succ = new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    if (StatusCodeUtils.dealStatusCode(s, GroupMemberActivity.this)) {
                        Gson gson = new Gson();
                        JSONObject jsonObject = new JSONObject(s);
                        List<MineInfo> templist = gson.fromJson(jsonObject.getString(HttpUtils.UPLOAD_DATA), new TypeToken<List<MineInfo>>() {
                        }.getType());
                        processData(templist);
                    } else {
                        mProgressbarHelper.onFailed();
                        refreshListView.onRefreshComplete();
                        is_load = false;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    mProgressbarHelper.onFailed();
                    refreshListView.onRefreshComplete();
                    is_load = false;
                }
            }
        };

        Response.ErrorListener error = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                is_load = false;
                mProgressbarHelper.onFailed();
                refreshListView.onRefreshComplete();
            }
        };
        OnErrorProxy errorProxy = new OnErrorProxy(error, succ, url, GroupMemberActivity.this, params);
        StringRequestParam stringRequest = new StringRequestParam(Request.Method.POST, url, params, succ, errorProxy);
        mQueue.add(stringRequest);
    }

    private void processData(List<MineInfo> templist) {
        if (page == 1) {
            list.clear();
        }
        list.addAll(templist);
        if (templist.isEmpty()) {
            is_end = true;
            if (page == 1) {
                is_load = false;
                mProgressbarHelper.onNodata("", true);
                refreshListView.onRefreshComplete();
            }
        }
        if (null == adapter) {
            adapter = new FollowAdapter(this, list);
            mListView.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
        is_load = false;
        mProgressbarHelper.onSuccess();
        refreshListView.onRefreshComplete();
    }

    @Override
    public void onBackClick() {
        finish();
    }

    @Override
    public void onActionClick(int id) {

    }
}
